﻿using System.Collections.ObjectModel;
using Microsoft.Xna.Framework.Input;

namespace HMEngine.HMInputs {
    public delegate void HMKeyPressedHandler(Collection<Keys> keys);
    public delegate void HMKeyHeldHandler(Collection<Keys> keys);
    public delegate void HMKeyReleasedHandler(Collection<Keys> keys);

    public class HMKeyboardDevice : HMInputDevice {
        private KeyboardState lastState;
        private Collection<Keys> lastPressedKeys;

        private Collection<Keys> pressed;
        private Collection<Keys> held;
        private Collection<Keys> released;

        public event HMKeyPressedHandler OnKeyPressed;
        public event HMKeyHeldHandler OnKeyHeld;
        public event HMKeyReleasedHandler OnKeyReleased;

        internal override void Initialize() {
            lastState = Keyboard.GetState();
        }

        internal override void Update() {
            KeyboardState currentState = Keyboard.GetState();
            Collection<Keys> currentPressedKeys = new Collection<Keys>(currentState.GetPressedKeys());
            lastPressedKeys = new Collection<Keys>(lastState.GetPressedKeys());
            lastState = currentState;

            pressed = new Collection<Keys>();
            held = new Collection<Keys>();
            released = new Collection<Keys>();

            // Loading Event Lists
            foreach (Keys key in currentPressedKeys) {
                if (lastPressedKeys.Contains(key)) held.Add(key);
                else pressed.Add(key);
            }

            foreach (Keys key in lastPressedKeys) {
                if (!currentPressedKeys.Contains(key)) released.Add(key);
            }

            // Event Calls
            if (pressed.Count > 0 && null != OnKeyPressed) OnKeyPressed(pressed);
            if (held.Count > 0 && null != OnKeyHeld) OnKeyHeld(held);
            if (released.Count > 0 && null != OnKeyReleased) OnKeyReleased(released);
        }
    }

}